<template>
    <base-breadcrumb :breadcrumb="!isAudit">
        <div class="about-master-container">
            <a-card v-loading="loading">
                <title-name title="基本信息"></title-name>
                <a-form-model :model="form" ref="formRules" :rules="formRules">
                    <a-row>
                        <a-col span="8">
                            <a-form-model-item label="层级">
                                <BSelect disabled v-model="form.level" :list="options.level"></BSelect>
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="单位">
                                <b-input v-model="form.depName" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="工会组织">
                                <SelectTreeUnion v-model="form.orgId" :isAuth="false" disabled @getOption="getOption"></SelectTreeUnion>
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="工作职务">
                                <b-input v-model="form.projectDuty" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="姓名">
                                <b-input v-model="form.name" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="性别">
                                <b-input v-model="form.sex" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="身份证号码">
                                <b-input :value="form.idCard" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="联系电话">
                                <b-input v-model="form.telphone" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="入职时间">
                                <b-input v-model="form.entryTime" disabled />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="家庭人口" prop="familyNum">
                                <b-input-number :min="0" v-model="form.familyNum" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="家庭月收入(元)" prop="familyMonthlyIncome">
                                <b-input-number :min="0" :precision="2" v-model="form.familyMonthlyIncome" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="人均月收入(元)" prop="avgMonthlyIncome">
                                <b-input-number :min="0" :precision="2" v-model="form.avgMonthlyIncome" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="当地最低工资标准(元)" prop="lastSalary">
                                <b-input-number :min="0" :precision="2" v-model="form.lastSalary" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="困难级别" prop="hardLevel">
                                <a-select v-model="form.hardLevel" :disabled="disabled">
                                    <a-select-option value="公司工会帮扶">公司工会帮扶</a-select-option>
                                    <a-select-option value="局工会重点帮扶">局工会重点帮扶</a-select-option>
                                </a-select>
                            </a-form-model-item>
                        </a-col>
                        <a-col span="24">
                            <a-form-model-item label="致困原因" prop="hardResult">
                                <b-textarea v-model="form.hardResult" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="24">
                            <a-form-model-item label="动态变化">
                                <b-textarea v-model="form.dynamicChange" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="24">
                            <a-form-model-item label="备注">
                                <b-textarea v-model="form.remark" :disabled="disabled" />
                            </a-form-model-item>
                        </a-col>
                        <a-col span="8">
                            <a-form-model-item label="附件" prop="fileInfoVos">
                                <b-upload uploadType="single"
                                          :disabled="disabled"
                                          businessCode="UN02" :fileList.sync="form.fileInfoVos" />
                                <span v-if="!disabled">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</span>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>
            <div class="tar page-btn-right-top">
                <a-button v-if="!disabled" type="primary" :loading="loading" @click="submit" class="mr10p">提交</a-button>
                <a-button v-if="!disabled" type="primary" :loading="loading" @click="save('DRAFT',false)" class="mr10p">保存
                </a-button>
                <a-button v-checkVisibleInDetail @click="$close">关闭</a-button>
            </div>
        </div>
        <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
</template>

<script>
import hardWorkApi from '@/api/union/employeeCare/hardWorkApi'
import SelectTreeUnion from '@/components/Select/SelectTreeUnion'
import BSelect from '@/components/BComponents/BSelect'

export default {
    name:'a'+Date.now(),
    components: {
        SelectTreeUnion,
        BSelect
    },
    props: {
        isAudit: { // 是否作为审批详情
            type: Boolean,
            default: false
        }
    },
    watch: {
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.disabled = true
                }
            }
        }

    },
    data() {
        let projectDuty = ''
        if (this.$userInfo().employee.posts) {
            projectDuty = this.$userInfo().employee.posts.map(v => {
                return v.orgName + '-' + v.name
            }).join('/')
        }
        return {
            disabled: this.$route.query.detail == 1,
            loading: false,
            form: {
                depId: this.$currentPost().deptId,
                depName: this.$currentPost().name,
                name: this.$userInfo().name,
                sex: this.$userInfo().employee.sex,
                telphone: this.$userInfo().employee.phone,
                entryTime: this.$userInfo().updateTime,
                idCard: this.$userInfo().employee.idCard,
                projectDuty,
                level: '',
                orgId: '',
                companyId: '',
                branchComId: '',
                projectId: '',
                'avgMonthlyIncome': '',
                'branchComName': '',
                'companyName': '',
                'dynamicChange': '',
                'familyMonthlyIncome': '',
                'familyNum': '',
                'fileInfoVos': [],
                'hardLevel': '',
                'hardResult': '',
                'lastSalary': '',
                'orgName': '',
                'projectName': '',
                'remark': ''
            },
            formRules: {
                avgMonthlyIncome: { required: true, message: '人均月收入不能为空', trigger: 'blur' },
                dynamicChange: { required: true, message: '动态变化不能为空', trigger: 'change' },
                entryTime: { required: true, message: '入职时间不能为空', trigger: 'change' },
                familyMonthlyIncome: { required: true, message: '家庭月收入不能为空', trigger: 'blur' },
                familyNum: { required: true, message: '家庭人口不能为空', trigger: 'blur' },
                hardLevel: { required: true, message: '困难级别不能为空', trigger: 'change' },
                hardResult: { required: true, message: '致困原因不能为空', trigger: 'change' },
                lastSalary: { required: true, message: '当地最低工资标准不能为空', trigger: 'change' },
                orgId: { required: true, message: '组织不能为空', trigger: 'change' },
                projectDuty: { required: true, message: '项目岗位不能为空', trigger: 'change' },
                projectId: { required: true, message: '项目不能为空', trigger: 'change' },
                remark: { required: true, message: '备注不能为空', trigger: 'change' },
                telphone: { required: true, message: '联系电话不能为空', trigger: 'change' }
            },
            options: {
                level: []
            },
         //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null,
        }
    },

    created: async function() {
        await this.getTypeLabel({ type: 1 })
        await this.getUnionOrg()
        if (this.$route.query.id) {
            this.form.id = this.$route.query.id
            await this.getBaseInfo()
        }
     //获取通过获取流程配置值
       this.processConfig = await this.$getConfigFn('union_hard_audit') 
    },
    methods: {
        async getUnionOrg() {
            let res = await hardWorkApi.getLoginUnionOrg()
            if (res.code === this.SUCCESS_CODE) {
                if (res.data.projectId) return this.form.orgId = res.data.projectId
                if (res.data.branchComId) return this.form.orgId = res.data.branchComId
                if (res.data.companyId) return this.form.orgId = res.data.companyId
            }
        },
        getOption(e) {
            if (!e) return
            let levelId = e.unionLevelId // 层级id
            this.form.level = levelId
            this.form.orgName = e.unionOrgName
            /**
             * 添加表单组织机构参数
             */
            // 公司级
            if (levelId === '605004340528287745') {
                this.form.companyId = e.id
                // 分公司级
            } else if (levelId === '605004401341501446') {
                this.form.branchComId = e.id
                this.form.companyId = e.parentId
                // 项目级
            } else if (levelId === '605004528177254447') {
                this.form.projectId = e.id
                this.form.branchComId = e.parentId
            }
        },
        async getTypeLabel(args) {
            const { unionApiUrl } = require('@/api/config.js')
            const Union = require('@/api/union').default
            const { getLabels } = new Union(unionApiUrl, 'queryTypesLabelList')
            let res = await getLabels(args)
            this.options.level = res.data
        },
        async getBaseInfo() {
            this.loading = true
            let res = await hardWorkApi.findById({}, { id: this.form.id })
            if (res.code === this.SUCCESS_CODE) {
                this.form = res.data
                this.form.entryTime = this.$dateFormat(this.form.entryTime, 'D')
                this.form.fileInfoVos = this.form.fileInfoVos ? this.form.fileInfoVos : []
            }
            this.loading = false
        },
        async submit() {
            if (!await this.save('DRAFT', true)) {
                return
            }
            if (await this.save('SUBMITTED', true)) {
                //this.$close()
            }
        },
        async save(auditStatus, isSubmit = false) {
            return new Promise(resolve => {
                this.$refs.formRules.validate(async valid => {
                    if (valid) {
                        this.loading = true
                        let res
                        let temp = this.$clone(this.form)
                        temp.auditStatus = auditStatus
                        if (temp.entryTime) {
                            temp.entryTime = new Date(temp.entryTime).valueOf()
                        }
                       this.submitApprovalConfirm (auditStatus == 'SUBMITTED'?this.processConfig:6, async(flag) =>{
                          if(!flag) {
                            that.loading = false
                            return
                          }
                         if (temp.id) {
                            res = await hardWorkApi.update(temp)
                        } else {
                            res = await hardWorkApi.add(temp)
                        }
                        if (res.code === 200) {
                            if (auditStatus === 'DRAFT') {
                                this.$message.success('保存成功')
                            } else {
                                this.$message.success('提交成功')
                                this.businessId = res.data.id;
                                this.setProcessConfig = this.processConfig;
                            }
                            this.form.id = res.data.id
                            await this.getBaseInfo()
                            if (!isSubmit) {
                                this.$close()
                            }
                            resolve(true)
                        }
                        this.loading = false
                       })
                    
                    } else {
                        this.$noticeWarning('请填写完整')
                    }
                })
            })
        }

    }
}
</script>
